﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using PhysicalDataProvider;

namespace FAQFrameworks.Framewors
{
    /*
     interface IEventService
    {
        Guid AddEvent(IDataElement de);

        IDataElement GetEvent(Guid id);
    }

    
   public class EventDBDataElment : CustomDataElement
   {
       public EventDBDataElment(IDataElement de)
           : base(de)
       {

       }

       public override IDataElement this[string attrName]
       {
           get
           {
               switch (attrName)
               {
                   case "EventId":
                       return GetDBId(attrName, true);
                   case "UserId":
                       return GetDBId(attrName, false);
                   case "Time":
                       return GetCurrentTime();
                   case "IP":
                       return GetString(attrName);
                   case "Action":
                       return GetInt(attrName);
                   case "Description":
                       return GetString(attrName);
                   case "TargetId":
                       return GetDBId(attrName, false);
                   default:
                       break;
               }
               return base[attrName];
           }
           set
           {
               base[attrName] = value;
           }
       }

       private IDataElement GetInt(string attrName)
       {
           IDataElement de = base[attrName];
           int id = Convert.ToInt32(de);
           return IntDataElement(id);
       }

       private IDataElement GetString(string attrName)
       {
           IDataElement de = base[attrName];
           return new StringDataElement(de.ToString());
       }

       private IDataElement GetCurrentTime()
       {
           return new ObjectDataElment(DateTime.Now);
       }

       private IDataElement GetDBId(string attrName, bool allowCreate)
       {
           IDataElement idDE = base[attrName];
           if (idDE.IsEmpty && allowCreate)
           {
               Guid guid = Guid.NewGuid();
               string str = Convert.ToBase64String(guid.ToByteArray());

               return new StringDataElement(str);
           }
           if (!idDE.IsEmpty)
           {
               if (idDE.Value is Guid)
               {
                   Guid guid = (Guid)idDE.Value;

                   string str = Convert.ToBase64String(guid.ToByteArray());
               }

               string id = idDE.Value.ToString();
               if (id.Length == 36)
               {
                   Guid guid = new Guid(id);

                   string str = Convert.ToBase64String(guid.ToByteArray());

                   return new StringDataElement(str);
               }
               else if (id.Length == 24)
               {
                   return new StringDataElement(id);
               }
           }

           throw new DataElementException(attrName + "数据为" + idDE.Value);
       }
   }

   
   public class EventService : IEventService
   {


       #region IEventManager 成员

       public Guid AddEvent(IDataElement de)
       {
           Guid guid = Guid.NewGuid();
           de["EventId"] = new ObjectDataElement(guid);

           EventDBDataElment dbDe = new EventDBDataElment(de);



           return guid;
       }

       string[] cols
           = new string[] { "EventId", "UserId", "Time", "IP","Action", 
               "Description","TargetId"};

       string table = "t_Events";

       public IDataElement GetEvent(Guid id)
       {
           IDataElement de = DataProvider.Select
               (cols, table,
               "EventId='" + Convert.ToBase64String(id.ToByteArray()) + "'", idCols);

           return de;
       }

       #endregion
   }
    * */
}
